<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use Markdown;
use App\Model\Art;
use Auth;
use captcha;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 无model层写法 直接使用DB进行查库
        // $arts = DB::table('arts')->orderBy('id','desc')->paginate(2);

        // 实例化对象
        $art = new Art;
        $arts = $art->paginate(5);

        // 静态方法调用 

        // $arts = Art::paginate(2);

        return view('art.index',['arts'=>$arts]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('art.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */


    public function store(Request $request)
    {
        $data = $request->except('_token');

        // $res = DB::table('arts')->insert($data);
        // $art = new Art();
        // $art->title = $request->title;
        // $art->description = $request->description;
        // $art->save();
        // return redirect('/art');

        // 第二种
        $art = Art::create(['title' => $request->title,'description'=>$request->description]);

        return redirect('/art');

  
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // $art = DB::table('arts')->where('id','=',$id)->first();
        // $art->description= Markdown::convertToHtml($art->description);

        $art = Art::find($id);
        return view('art.show',['art'=>$art]);
    }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // $art = DB::table('arts')->where('id','=',$id)->first();
        $art = Art::find($id);
        return view('art.edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        /*$res = DB::table('arts')->where('id','=',$id)
            ->update(['title'=>$request->title,'description'=>$request->description]);*/
        // 第一种方法
        $art = Art::find($id);
        $art->title = $request->title;
        $art->description = $request->description;
        $art->save();

        // 第二种方法
        // $art = Art::where('id',$id)->update($request->except('_token','_method'));

        return redirect('/art');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */


    public function destroy($id)
    {
       // $res = DB::table('arts')->where('id',$id)->delete();
        // 第一种方法
        /*$art = Art::find($id);
        $art->delete();
        */

        // 第二种方法

        $art = Art::destroy($id);
        return redirect('/art');
    }

    public function say($num)
    {
        echo 'func say';
        echo "<br>";
        echo $num;
    }

    public function getAbc()
    {
        echo 'abc';
    }

    public function postAb()
    {
        echo 'ab';
    }

    public function getMk()
    {
        $str="## h1";
        echo Markdown::convertToHtml($str);
    }

    public function getLogout()
    {
        Auth::logout();
        return redirect('auth/login');
    }

    public  function postLogin(Request $request)
    {

        $res = Auth::attempt(['email' => $request->email, 'password' => $request->password]);

       if($res){
           return redirect('/art');
       }else{
           return back();
       }
    }

    /*public function getImg()
    {
        return captcha_src();
    }
*/
}
